package android.newnoe;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.newnoe.Fragment.DongFragment;
import android.newnoe.Fragment.RenFragment;
import android.newnoe.Fragment.ShouFragment;
import android.newnoe.Fragment.TiFragment;
import android.newnoe.Fragment.WenFragment;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {
    private RadioGroup radioGroup;
    private RadioButton radioButton;

    private ShouFragment shouFragment = null;
    private WenFragment wenFragment = null;
    private RenFragment renFragment = null;
    private TiFragment tiFragment = null;
    private DongFragment dongFragment = null;

    private FragmentManager fm = null;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getFragmentManager();

        radioGroup = (RadioGroup) findViewById(R.id.rb_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction ft = fm.beginTransaction();
                Hand(ft);

                switch (checkedId) {
                    case R.id.rb_btn1:
                        if (shouFragment == null) {
                            shouFragment = new ShouFragment();
                           ft.add(R.id.fl_main,shouFragment);
                        }
                        else {
                            ft.show(shouFragment);
                        }
                        break;
                    case R.id.rb_btn2:
                        if (null == wenFragment){
                            wenFragment = new WenFragment();
                            ft.add(R.id.fl_main,wenFragment);
                        }else {
                            ft.show(wenFragment);
                        }
                        break;
                    case R.id.rb_btn3:
                        if (tiFragment == null){
                            tiFragment = new TiFragment();
                            ft.add(R.id.fl_main,tiFragment);
                        }else {
                            ft.show(tiFragment);
                        }
                        break;
                    case R.id.rb_btn4:
                        if (dongFragment == null){
                            dongFragment = new DongFragment();
                            ft.add(R.id.fl_main,dongFragment);
                        }else {
                            ft.show(dongFragment);
                        }
                        break;
                    case R.id.rb_btn5:
                        if (renFragment == null){
                            renFragment = new RenFragment();
                            ft.add(R.id.fl_main,renFragment);
                        }else {
                            ft.show(renFragment);
                        }
                        break;
                }
            }
        });
        radioButton = (RadioButton) findViewById(R.id.rb_btn1);
        radioButton.setChecked(true);
        }


    private void Hand(FragmentTransaction ft) {
        if (null!=shouFragment){
            ft.hide(shouFragment);
        }
        if (null!=wenFragment){
            ft.hide(wenFragment);
        }
        if (null!=tiFragment){
            ft.hide(tiFragment);
        }
        if (null!=dongFragment){
            ft.hide(dongFragment);
        }
        if (null!=renFragment){
            ft.hide(renFragment);
        }
    }
}